﻿<?php
/**
 Template Name:game_list
 * @package WordPress
 * @subpackage Default_Theme
 * @author: shiwei
 */

	/**
	 * 使用方法提示===
	 * 后台新建page页使用此页为模块（game_list），用新建的page链接替换$selfPageLink值。
	 * 
	 * 如果外部调用必须提供两个参数
	 * 1、keyword :搜索关键字
	 * 2、$category_id：父类型ID(手机游戏、flash游戏等的ID)
	 * 
	 * 
	 * 
	 */

/**
 * 截取字符串（中文算1个长度）
 * @param $str
 * @param $lenth
 * @param $start
 * @return unknown_type
 */
function subString_UTF8($str, $lenth, $start=0){  
	$len = strlen($str);  
	$r = array();  
	$n = 0;  
	$m = 0;  
	for($i = 0; $i < $len; $i++) {  
		$x = substr($str, $i, 1);  
		$a  = base_convert(ord($x), 10, 2);  
		$a = substr('00000000'.$a, -8);  
		if ($n < $start){  
			if (substr($a, 0, 1) == 0) {  
			}elseif (substr($a, 0, 3) == 110) {  
				$i += 1;  
			}elseif (substr($a, 0, 4) == 1110) {  
				$i += 2;  
			}  
			$n++;  
		}else{  
			if (substr($a, 0, 1) == 0) {  
				$r[ ] = substr($str, $i, 1);  
			}elseif (substr($a, 0, 3) == 110) {  
				$r[ ] = substr($str, $i, 2);  
				$i += 1;  
			}elseif (substr($a, 0, 4) == 1110) {  
				$r[ ] = substr($str, $i, 3);  
				$i += 2;  
           	}else{  
            	$r[ ] = '';  
            }  
            if (++$m >= $lenth){  
            	break;  
            }  
       	}  
	}  
    return join('',$r); 
} 
	
	
/**
 * 句子的关键字高亮显示
 * @param $str
 * @param $words
 * @return unknown_type
 */
function highlight_word($str, $words){
	if(is_array($words)) {          
		foreach($words as $k => $word){    
         	$pattern[$k] = '/(?!<[^>]*)'.
				'('.$word.')'.
				'(?![^<]*>)/ui';   
        	$replace[$k] = "<font style='color:red'>\\1</font>"; 
        }    
    } else {    
       	$pattern =  '/(?!<[^>]*)'.
				'('.$word.')'.
				'(?![^<]*>)/ui';
     	$replace = "<font style='color:red'>\\1</font>";  
    }        
	return preg_replace($pattern,$replace,$str);  
}



	//全局参数()；
	$pageSize = 10 ;
	$pageModel= 10 ; //不允许改变的值
	$selfPageLink = "?page_id=33209" ; //自身链接；
	$autoLink = "" ;
	$siteurl = get_bloginfo('template_url');

	//父类型id
	$category_id = $_POST['category_id'] ; 
	if($category_id == null || "" == $category_id){
		$category_id = $_GET['category_id'] ;
	}
	$categories = "" ;
	$catStr = "" ;
	if($category_id != null && "" != $category_id){
		$categories = get_categories ('child_of='.$category_id) ;
//		$categories = $wpdb->get_results("select p.name,p.term_id as  pterm_id ,t.name,t.term_id as term_id ".
//			" from wp_terms t ,wp_term_taxonomy m , wp_terms p ".
//			" where t.term_id = m.term_id and p.term_id = m.parent and p.term_id  ='".$category_id."'") ;
		foreach ($categories as $category) {
			$catStr =$catStr."'".$category->term_id."'," ;
		}
	}
	
	//搜索关键字
	//$keyword = $_POST('keyword') ;
	$keyword = $_POST['keyword'] ;
	if($keyword == null || "" == $keyword ){
		$keyword = $_GET["keyword"]; 
	}
	if($keyword == null){
		$keyword = "" ;
	}
	$keyword = trim($keyword) ;
	$keyword = preg_replace("/\s+/" ," "  ,$keyword) ;
	//页数
	$paged = $_POST["paged"]; 
	if($paged == null || "" == $paged ){
		$paged = $_GET["paged"]; 
		if($paged == null || "" == $paged ){
			$paged = 0 ;
		}
	}
	//这里首先查询出所有类型
	$queryStr = "select distinct terp.term_id as term_id, terp.name as name from wp_term_taxonomy tax ,wp_terms terp where tax.parent = terp.term_id" ;
	$cats = $wpdb->get_results($queryStr );
	
	$statNum = $pageSize * $paged ;
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="description" content="<?php echo $keyword;?>，游戏搜索引擎，Flash小游戏搜索, 网页游戏搜索, 手机游戏搜索。在这里寻找上万款游戏！" />
		<meta name="keywords" content="<?php echo $keyword;?>, 游戏, 搜索, flash 游戏, 网页游戏, 手机游戏, 搜索引擎" />
		<title><?php echo $keyword;?> | <?php echo get_bloginfo('name');?></title>
		<script src="<?php echo $siteurl ?>/js/lib/jquery.js" type="text/javascript"></script>
		<script src="<?php echo $siteurl ?>/js/lib/jquery.autocomplete.js" type="text/javascript"></script>
		<script src="<?php echo $siteurl ?>/js/lib/function.js" type="text/javascript"></script>
		<link href="<?php echo $siteurl ?>/js/lib/css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
 		<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
		
	</head>
	<body>
		<div id="list_cont">
			<div id="top">
				<ul>
					<?php 
					
						if($category_id != null && "" != $category_id){
					?>
							<li class="menuNo"><a href="<?php echo $selfPageLink ;?>">全部游戏</a></li>
					<?php 
						}else{
					?>
							<li class="menuOn">全部游戏</li>
					<?php 
						}
					?>
					<?php 
						foreach($cats as $v){
							if($v->term_id == $category_id){
					?>
								<li class="menuOn" ><?php echo $v->name; ?></li>
					<?php 	
							}else{
					?>
								<li class="menuNo"><a href="<?php echo $selfPageLink."&category_id=".$v->term_id."&keyword=".$keyword ;?>" ><?php echo $v->name; ?></a></li>
					<?php 			
							}
						}
					?>
					<!-- 
					<li class="menuNo"><a href="#">网页游戏</a></li>
					<li class="menuNo"><a href="#">flash游戏</a></li>
					<li class="menuNo"><a href="#">手机游戏</a></li> -->
				</ul>
			</div>
			<p id="login"><a href="'#">登录</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">注册</a></p>
			<div id="ganme_soso" class="list_soso">
				<a href="/" ><img src="<?php echo $siteurl ?>/images/smalllogo.jpg" width="251" height="41" id="smalllogo" alt="游戏搜搜" title="游戏搜搜" /></a>
				<ul class="sousuo">
					<form name="search" action="<?php echo $selfPageLink ;?>" method="post">
						<input id="category_id" type="hidden" name="category_id" value="<?php echo $category_id;?>" />
						<li class="soso_li"><input type="text" class="soso_text" name="keyword" id="keyword" value="<?php if($keyword != null && "" != $keyword){echo $keyword ;}else{echo '请输入要检索的游戏名称';}?>" onclick="clear(1)" onfocus="if(this.value == '请输入要检索的游戏名称') {this.value='';}init_search('keyword');" onblur="if(this.value=='')this.value='请输入要检索的游戏名称';" /></li> 
						<li><input type="submit" value="搜索" class="soso_button"  /></li>
					</form> 
					<a href="#"><img src="<?php echo $siteurl?>/images/suibianwanwan.gif" id="suibianplay" alt="游戏搜搜" title="游戏搜搜" /></a>
				</ul>		
			</div>
			<div id="list_left">
				<ul>
					<li id="xuanzhong">>快速分类</li>
					<?php 
					
						if($category_id != null && "" != $category_id){
					?>
							<li class="fenleileftfloat NOxuanzhong" ><a href="<?php echo $selfPageLink."&keyword=".$keyword  ;?>">全部游戏</a></li>
					<?php 
						}else{
					?>
							<li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink ;?>">全部游戏</a></li>
					<?php 
						}
					?>
					<?php 
						foreach($cats as $v){
							if($v->term_id == $category_id){
					?>
								<li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."&category_id=".$v->term_id."&keyword=".$keyword ;?>" ><?php echo $v->name; ?></a></li>
					<?php 	
							}else{
					?>
								<li class="fenleileftfloat NOxuanzhong"><a href="<?php echo $selfPageLink."&category_id=".$v->term_id."&keyword=".$keyword ;?>"><?php echo $v->name; ?></a></li>
					<?php 			
							}
						}
					?>
					<!-- 
					<li class="NOxuanzhong"><a href="#">网页游戏</a></li>
					<li class="NOxuanzhong"><a href="#">flash游戏</a></li>
					<li class="NOxuanzhong"><a href="#">手机游戏</a></li> -->
				</ul>
			</div>
			<div id="list_center">
				<!--  搜索结果列表开始 -->
				<?php 
					$condition = " where post.ID = ships.object_id and ships.term_taxonomy_id = tax.term_taxonomy_id and post_status = 'publish' AND post_type = 'post' " ;
					if($keyword != null && "" != $keyword){
						$condition=$condition." and post_title like '%".$keyword."%' ";
					}
					if($category_id != null && "" != $category_id){
						if($catStr !=null && "" != $catStr){
							$catStr = substr($catStr,0,strlen($catStr)-1) ;
							$condition=$condition." and tax.term_id in (".$catStr.") " ;
						}else{
							$condition=$condition." and tax.term_id = '' " ;
						}
					}
					$postCountQue = "select count(distinct post.post_title) as counts from  wp_term_taxonomy tax ,wp_term_relationships ships, wp_posts post ".$condition ;
					$mycounts = $wpdb->get_results($postCountQue) ;
					$mycount= 0 ;
					if($mycounts != null){
						$mycount = $mycounts[0]->counts ;
					}
					
				?>
				<span id="mianbaoxie" class="list_centtopspan">当前位置：<a href="?">首页</a>=><a href="<?php echo $selfPageLink ;?>">游戏搜索</a></span>
				<span id="soso_result" class="list_centtopspan">找到<?php echo $mycount; ?>个相关游戏</span>
				<?php 
					//$args = array ('paged'=>$paged,"orderby"=>"date");	
					//query_posts($args);
					//while (have_posts()) : the_post(); 
					
					$postQue = "select count(distinct post.post_title) ,post.ID as ID ,post.post_title as post_title ,tax.term_id as term_id ,post.post_content as post_content ".
						"from wp_term_taxonomy tax ,wp_term_relationships ships, wp_posts post ".$condition." group by post.post_title  ORDER BY post.post_date DESC LIMIT ".$statNum.",".$pageSize ;
					$myposts = $wpdb->get_results($postQue);
					$keyArray = explode(" ",$keyword) ;
					foreach($myposts as $mypost) {
						// get game image by user defined
						//$gameSize = get_post_meta($mypost->ID, 'GameSize', true);  
						$gamePic = get_post_meta($mypost->ID, 'GamePic', true);  
						$gameLink = get_post_meta($mypost->ID, 'GameLink', true);  
						$postLink = get_permalink($mypost->ID);
						$postTitle = $mypost->post_title ; 
						//$titleArray = explode($keyword,$postTitle) ;
						// bug!!!!
//						$titleHtml = highlight_word($postTitle,$keyArray) ; 
//						$desContent = $mypost->post_content ;
//						$desHtml =  subString_UTF8($desContent,150),$keyArray) ;

					  
						$desContent = $mypost->post_content ;
						$desHtml =  subString_UTF8($desContent,150) ;
//						echo $titleHtml ;
				?>
					<!-- addfadfsafasfs shiwei 0505 -->
						<div class="list">
							<span class="list_img"><a href="<?php echo $postLink; ?>"><img src="<?php echo $gamePic ?>" width="104" height="79" alt="<?php echo $postTitle;?>" title="<?php echo $postTitle;?>" /></a></span>
							<h3 class="list_title"><a href="<?php echo $postLink; ?>"><?php echo $postTitle;//$mypost->post_title; ?></a></h3>
							<span class="list_cat">							
				         <?php 
				           $cat_name = get_cat_name($mypost->term_id);
				           if($cat_name == 'andriod' ||$cat_name == 'iphone' || $cat_name == 'windows phone' ){
				           	echo "游戏平台:";
				           }else {
				           	echo "类别:";
				           }
							?>
							<samp><?php echo $cat_name?></samp></span>
							<p class="list_neirong"><?php echo $desHtml.'... ...'; ?></p>
							<span class="list_add">游戏源地址：<!-- <a href="<?php //echo $gameLink; ?>"> --><samp><?php echo $gameLink; ?></samp><!-- </a> --></span>
						</div>
				<?php 
					}
					//endwhile;
				?>
				<!--  搜索结果列表结束 -->
				
				<!--  分页开始 -->
				
				<div id="scroll">
					<?php 
					$totalPage = ceil($mycount/$pageSize) ;
					if($totalPage >0){
						$i = 0 ;
						$pagedLink = $selfPageLink."&keyword=".$keyword."&category_id=".$category_id ;
						if(intval($paged) > 0 ){		
					?>
							<a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>" ><img src="<?php echo $siteurl ?>/images/next-top.gif" alt="上一页" title="上一页" class="f_l"/></a>
					<?php 
						}
					?>
					<?php 
						if($totalPage<=$pageModel){
						 	for(;$i<$totalPage;$i++){
						 		if($i == intval($paged)){
					?>
									<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
						 		}else{
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
						 		}
						 	}
						 		
						}elseif($totalPage < $pageModel + 3){
							$halfPageModel = intval($pageModel /3)  ;
							if(intval($paged) < $halfPageModel*2){
								
								for($i=0;$i < $halfPageModel*2+1;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage-($pageModel-$halfPageModel*2 -2);$i<$totalPage;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}	
					?>
					<?php 
							}elseif(intval($paged) > $halfPageModel - 1) {
								for($i =0 ;$i< $halfPageModel-1;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 	
								for($i=$totalPage-($pageModel-$halfPageModel);$i < $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
							}
						}else{
							$halfPageModel = intval($pageModel /2)  ;
							if(intval($paged) < $halfPageModel-1){
								for($i =0 ;$i< $halfPageModel;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 			
								for($i =$totalPage - ($pageModel - $halfPageModel - 1) ;$i< $totalPage ;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
							}elseif(intval($paged) > $totalPage - ($halfPageModel) ) {
								for($i = 0 ;$i< $halfPageModel-1 ;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - ($pageModel - $halfPageModel) ;$i< $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}	
								
							}else {
					?>
								<a href="<?php echo $pagedLink."&paged=0"; ?>" class="linkstyle03"><?php echo 1;?></a>
								<a href="<?php echo $pagedLink."&paged=1"; ?>" class="linkstyle03"><?php echo 2;?></a>
					<?php 
								$uses = 3 ;
								if($paged > $totalPage/2){
									$uses = 2 ;
					?>
									<a href="<?php echo $pagedLink."&paged=1"; ?>" class="linkstyle03"><?php echo 3;?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
								<a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>" class="linkstyle03"><?php echo intval($paged);?></a>
								<font class="linknone"><?php echo (intval($paged) +1);?></font>
								<a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>" class="linkstyle03"><?php echo (intval($paged) +2);?></a>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - $uses ;$i< $totalPage;$i++){
					?>
										<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
								}
							}
						}						
						if(intval($paged)<$totalPage-1){
					?>
							<a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>" ><img src="<?php echo $siteurl ?>/images/next-bottom.gif" alt="下一页" title="下一页" class="f_l" /></a>
					<?php 
						}
					}
					?>
				</div>
				
				<!--  分页结束 -->
				
				
			</div>
			
			<!-- 热门游戏开始 -->
			<div id="list_right">
				<h2>热门游戏推荐</h2>
				<?php 
					$hotPostQue = "select post.ID as ID ,post.post_title as post_title ,tax.term_id as term_id ,post.post_content as post_content ".
						" from wp_term_taxonomy tax ,wp_term_relationships ships, wp_posts post where ".
						" post.ID = ships.object_id and ships.term_taxonomy_id = tax.term_taxonomy_id and post_status = 'publish' AND post_type = 'post' ".
						" order by post.comment_count desc limit 0,10 ";
					//query_posts('orderby=comment_count&showposts=4');//返回分类ID为7的2篇内容
					//while (have_posts()) : the_post(); 
					$i =1 ;
					$hotposts = $wpdb->get_results($hotPostQue);
					foreach($hotposts as $hotpost) {
						//$gameSize = get_post_meta($hotpost->ID, 'GameSize', true);  
						$gamePic = get_post_meta($hotpost->ID, 'GamePic', true);  
						//$gameLink = get_post_meta($hotpost->ID, 'GameLink', true);  
						$postLink = get_permalink($hotpost->ID);
				?>
						<div class="list_tuijian">
							<span class="list_img"><a href="<?php echo $postLink; ?>"><img src="<?php echo $gamePic; ?>" width="104" height="79" alt="<?php echo $hotpost->post_title;  ?>" title="<?php echo $hotpost->post_title;  ?>" /></a></span>
							<!-- <span class="listriht listriht_tuijian"><img src="<?php echo $siteurl ?>/images/tuijian/<?php echo  $i++ ;?>.jpg" /></span> -->
							<h5 class="listriht listriht_tit"><a href="<?php echo $postLink; ?>"><?php echo $hotpost->post_title;  ?></a></h5>
							<span class="listriht listriht_cat "><?php echo get_cat_name($hotpost->term_id);?></span>
						</div>
				<?php 
					}
					//endwhile;
				?>
			</div>
			<!-- 热门游戏结束 -->
		</div>	
<?php get_footer();?>
